package net.danlew.android.joda.sample.slice;

import net.danlew.android.joda.sample.DateUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

import ohos.global.icu.text.DateFormat;
import org.joda.time.DateTime;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static ohos.global.icu.text.DateFormat.*;

public class sampleFormatDateTimeslice extends AbilitySlice {
    public static String FORMAT_SHOW_TIME =HOUR_MINUTE;
    public static String FORMAT_SHOW_WEEKDAY =WEEKDAY;
    public static String FORMAT_SHOW_YEAR = YEAR;
    public static String FORMAT_SHOW_DATE = DateFormat.DAY;
    public static String FORMAT_NO_MONTH_DAY = DateFormat.YEAR_MONTH;
    public static String FORMAT_ABBREV_WEEKDAY = DateFormat.ABBR_WEEKDAY;
    public static String FORMAT_ABBREV_MONTH = DateFormat.ABBR_MONTH;
    public static String FORMAT_NUMERIC_DATE = DateFormat.NUM_MONTH_WEEKDAY_DAY;
    public static String FORMAT_ABBREV_ALL = DateFormat.ABBR_MONTH_DAY; //略有不同
    private DirectionalLayout directionalLayout = new DirectionalLayout(this);
    private DirectionalLayout.LayoutConfig layoutConfigD = new DirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT,
            ComponentContainer.LayoutConfig.MATCH_CONTENT);
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        directionalLayout.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
        directionalLayout.setHeight(ComponentContainer.LayoutConfig.MATCH_PARENT);
//            scrollView.setOrientation(Component.VERTICAL);
        directionalLayout.setPadding(32, 32, 80, 80);

        ShapeElement element = new ShapeElement();

        element.setRgbColor(new RgbColor(255, 255, 255));
        directionalLayout.setBackground(element);

        Text welcome = new Text(this);

        welcome.setLayoutConfig(layoutConfigD);
        welcome.setText("sampleFormatDateTime测试结果：");
        welcome.setTextSize(85);
        welcome.setTextColor(Color.RED);
        welcome.setMultipleLine(true);
        directionalLayout.addComponent(welcome);
        sampleFormatDateTime();
        super.setUIContent(directionalLayout);
    }
    private void sampleFormatDateTime() {
        List<String> text = new ArrayList<String>();
        DateTime now = DateTime.now();
        text.add("Show time: " + DateUtils.formatDateTime(this, now, FORMAT_SHOW_TIME));
        text.add("Show date: " + DateUtils.formatDateTime(this, now, FORMAT_SHOW_DATE));
        text.add("Numeric date: " + DateUtils.formatDateTime(this, now, FORMAT_NUMERIC_DATE));
        text.add("Show date (abbreviated): " + DateUtils.formatDateTime(this, now, FORMAT_ABBREV_ALL));
//        text.add("Show date and time: " + DateUtils.formatDateTime(this, now, FORMAT_SHOW_DATE
//            | DateUtils.FORMAT_SHOW_TIME));
//        text.add("Show date (force year): " + DateUtils.formatDateTime(this, now, DateUtils.FORMAT_SHOW_DATE
//            | DateUtils.FORMAT_SHOW_YEAR));
        addSample("DateUtils.formatDateTime()", text);
    }
    private void addSample(CharSequence title, Iterable<String> text) {
        addSample(title, join("\n",text));
    }
    public static String join( CharSequence delimiter,  Iterable tokens) {
        final Iterator<?> it = tokens.iterator();
        if (!it.hasNext()) {
            return "";
        }
        final StringBuilder sb = new StringBuilder();
        sb.append(it.next());
        while (it.hasNext()) {
            sb.append(delimiter);
            sb.append(it.next());
        }
        return sb.toString();
    }

    private void addSample(CharSequence title, CharSequence text) {

//        ((Text) view.findViewById(R.id.title)).setText(title);
//        ((Text) view.findViewById(R.id.text)).setText(text);
        Text titleText = new Text(this);
//            layoutConfig.gravity = LayoutAlignment.CENTER_HORIZONTAL;
        titleText.setLayoutConfig(layoutConfigD);
        titleText.setText((String) title);
        titleText.setTextColor(Color.BLUE);
        titleText.setTextSize(75);
        titleText.setMultipleLine(true);
        Text textT = new Text(this);
//            layoutConfig.gravity = LayoutAlignment.CENTER_HORIZONTAL;
        textT.setLayoutConfig(layoutConfigD);
        textT.setText((String) text);
        textT.setTextSize(70);
        textT.setMultipleLine(true);
        directionalLayout.addComponent(titleText);
        directionalLayout.addComponent(textT);
    }



}
